package com.zl.tby.screen.controller;

import com.zl.tby.dto.TbyCameraInfoQuery;
import com.zl.tby.dto.TbyCarRecordQuery;
import com.zl.tby.dto.TbyFaceRecordQuery;
import com.zl.tby.icc.IccDeviceInfo;
import com.zl.tby.icc.IccToken;
import com.zl.tby.icc.IccTokenCache;
import com.zl.tby.icc.IccUtil;
import com.zl.tby.model.TbyCarRecord;
import com.zl.tby.model.TbyFaceRecord;
import com.zl.tby.screen.service.ITbyCarMonitorScreenService;
import com.zl.tby.screen.vo.carMonitor.TbyCarMonitorBean;
import com.zl.tby.service.ITbyCarRecordService;
import com.zl.tby.service.ITbyFaceRecordService;
import com.zl.tby.vo.TbyFaceRecordForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;
import com.zl.tby.core.common.R;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 大屏车辆抓拍
 * @author changpeng
 * @version 1.0
 * @since 2024/9/19 11:57
 */
@Valid
@Controller
@Mapping("/car_monitor_screen")
public class TbyCarMonitorScreenController {

    @Inject
    private ITbyCarMonitorScreenService tbyCarMonitorScreenService;
    @Inject
    private ITbyCarRecordService tbyCarRecordService;
    @Inject
    private ITbyFaceRecordService tbyFaceRecordService;

    /**
     * 车辆抓拍统计
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/kk_statistic", method = MethodType.GET)
    public R<List<TbyCarMonitorBean>> kkStatistic(TbyCarRecordQuery query) {
        List<IccDeviceInfo> deviceList = IccUtil.deviceList(200, 5, null).getData();
        List<TbyCarMonitorBean> dataList = tbyCarMonitorScreenService.kkStatistic(query);
        Map<String, TbyCarMonitorBean> map = new HashMap<>();
        if (dataList != null && dataList.size() > 0) {
            for (TbyCarMonitorBean entity : dataList) {
                map.put(entity.getDeviceCode(), entity);
            }
        }
        List<TbyCarMonitorBean> resultList = new ArrayList<>();
        if (deviceList != null) {
            for (IccDeviceInfo device : deviceList) {
                if (map.containsKey(device.getDeviceCode())) {
                    resultList.add(map.get(device.getDeviceCode()));
                } else {
                    TbyCarMonitorBean data = new TbyCarMonitorBean();
                    data.setDataCount(0L);
                    data.setDeviceCode(device.getDeviceCode());
                    data.setDeviceName(device.getDeviceName());
                    resultList.add(data);
                }
            }
        }
        Collections.sort(resultList, new Comparator<TbyCarMonitorBean>() {
            @Override
            public int compare(TbyCarMonitorBean o1, TbyCarMonitorBean o2) {
                return o2.getDataCount().compareTo(o1.getDataCount());
            }
        });
        return R.ok(resultList);
    }

    /**
     * 车辆总数统计
     * @param query: 查询条件
     * @return 结果
     */
    @Mapping(value = "/car_number", method = MethodType.GET)
    public R<List<TbyCarMonitorBean>> carNumber(TbyCarRecordQuery query) {
        return R.ok(this.tbyCarMonitorScreenService.carNumber(query));
    }

    /**
     * 列表查询
     * @param query
     * @return
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyCarRecordQuery query) {
        return R.ok(this.tbyCarRecordService.page(query));
    }

    /**
     * 人脸列表
     * @param query：查询参数
     * @return
     */
    @Mapping(value = "/face_list", method = MethodType.GET)
    public R<?> faceList(@Body TbyFaceRecordQuery query) {
        return R.ok(this.tbyFaceRecordService.page(query));
    }

    /**
     * 人脸总数统计
     * @param query: 查询条件
     * @return 结果
     */
    @Mapping(value = "/face_number", method = MethodType.GET)
    public R<List<TbyCarMonitorBean>> faceNumber(TbyFaceRecordQuery query) {
        return R.ok(this.tbyCarMonitorScreenService.faceNumber(query));
    }

    /**
     * 人脸抓拍统计
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/face_statistic", method = MethodType.GET)
    public R<List<TbyCarMonitorBean>> faceStatistic(TbyFaceRecordQuery query) {
        return R.ok(tbyCarMonitorScreenService.faceStatistic(query));
    }

    @Mapping(value = "/face_detail", method = MethodType.GET)
    public R<TbyFaceRecord> faceDetail(Long id) {
        TbyFaceRecord record = this.tbyFaceRecordService.get(id);
        if (record != null) {
            IccToken token = IccTokenCache.getInstance().getToken();
            String tokenStr = token == null ? "" : token.getAccess_token();
            record.setBigImg(record.getBigImg() + "?token=" + tokenStr);
        }
        return R.ok(record);
    }
}
